﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Linq.Expressions;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;

namespace UCL.Powersource
{


    /// <summary>
    /// Based on Caliburn.Micro base class for PropertyChanged event raising objects
    /// </summary>
    [DataContract(IsReference=true)]
    public class PropertyChangedBase : INotifyPropertyChangedEx
    {
        public virtual void NotifyOfPropertyChange<TProperty>(Expression<Func<TProperty>> property)
        {
            RaisePropertyChanged(property.GetMemberInfo().Name);
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public void RaisePropertyChanged(string propertyName)
        {
            if (propertyName != null)
            {
                if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }


    [DataContract(IsReference = true)]
    public abstract class PropertyChangedInitializedLifeCycle : PropertyChangedBase, IInitiliazedLifeCycle
    {

        public void Initialize() { if (!IsInitialized) IsInitialized = OnInitializing(); }

        public bool IsInitialized { get; private set; }
        public bool IsDisposed { get; private set; }

        protected abstract bool OnInitializing();
        protected abstract void OnDisposing();

        public void Dispose()
        {
            Dispose(true);
        }

        private void Dispose(bool p)
        {
            if (p && !IsDisposed)
            {
                OnDisposing();
                IsDisposed = true;
                NotifyOfPropertyChange(() => IsDisposed);
                
            }
        }

     
    }

}
